
package com.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;

import java.io.UnsupportedEncodingException;

@Slf4j
public class Base64Util {


	/**
	 *
	 * @Title: decode
	 * @Description 解析字符串
	 * @param str
	 * @return byte[]
	 *
	 */
    public static byte[] decode(String str) {
        return Base64.decodeBase64(str.getBytes());
    }

    /**
     *
     * @Title: decode
     * @Description 解密字符串
     * @param str
     * @param charsetName 字符集
     * @return byte[]
     *
     */
    public static byte[] decode(String str,String charsetName) {

        try {
			return Base64.decodeBase64(str.getBytes(charsetName));
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage(),e);
		}
        return null;
    }
	/**
	 *
	 * @Title: encode
	 * @Description 加密字符串
	 * @param str
	 * @return String
	 *
	 */
    public static String encode(String str) {
        return new String(Base64.encodeBase64(str.getBytes()));
    }
    /** */
	/**
	 * <p>
	 * 二进制数据编码为BASE64字符串
	 * </p>
	 *
	 * @param bytes
	 * @return
	 * @throws Exception
	 */
	public static String encodes(byte[] bytes) throws Exception {
		return new String(Base64.encodeBase64(bytes));
	}

    /**
     *
     * @Title: encode
     * @Description 加密字符串
     * @param str
     * @param charsetName 字符集
     * @return String
     *
     */
    public static String encode(String str,String charsetName) {
        try {
			return new String(Base64.encodeBase64(str.getBytes(charsetName)));
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage(),e);
		}
        return null;
    }
  /*  public static void main(String[] args) throws Exception {
		String aa= "iVBORw0KGgoAAAANSUhEUgAAAFAAAABQCAMAAAC5zwKfAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABhQTFRF39/fyMjI+Pj47+/v0tLS2traxcXF////Yv6wpAAAAAh0Uk5T/////////wDeg71ZAAABLElEQVR42uyY2wrDMAxDlcu6///jwR5Gy+LETk9gg/jZqDVyJNt6wqENuAEXARbVlKoKBJiVjnckZQIwP45PPDIAqOMUug9Y0hkwlduAlx90/KJGnNYrYB3xrhGn6QqYRrxrxGkDsMu7Rpw2Su7yrhGn36T0edeIU88nOoCeAtu8G4AeClq8RwBdjRQp2dnqblKij1FxKQi1jUusIo3tktPQ03MJfkQcnJYUkK/f9uWI/1r5mvVfK1+z/mvla9Z/rXzN+q+Vr2n/NfLVFy47rHz1pLUXVv46QLxknBS8bfDG5p8eLg68fP3PJgWbFG2juNHTowg+LOHjHD5w4iMxPrTjawW++OCrGb488ustvoDzJ4IFR4zFZxb8EMSfqvBjGn/u2zfYDbgc8CXAACUVoCH9hvoSAAAAAElFTkSuQmCC";
		byte[] imagebyte =decode(aa);
		FileOutputStream out =new FileOutputStream("d:/aa.png");
		out.write(imagebyte);
		out.flush();
		out.close();
	}*/
}
